<template>
  <div class="merchant-layout">
    <!-- 顶部导航 -->
    <header class="merchant-header">
      <div class="header-container">
        <div class="logo">
          <img src="@/assets/jiang.jpg" alt="Logo" class="logo-img">
          <h2>商家中心</h2>
        </div>
        <div class="nav-menu">
          <router-link 
            v-for="item in navItems" 
            :key="item.path" 
            :to="item.path" 
            class="nav-item"
            :class="{ active: activeMenu === item.path }"
          >
            <el-icon><component :is="item.icon" /></el-icon>
            <span>{{ item.title }}</span>
          </router-link>
        </div>
        <div class="user-info">
          <el-dropdown trigger="click">
            <span class="user-dropdown">
              商家名称
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToShop">访问店铺</el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </header>

    <!-- 内容区域 -->
    <div class="content-container">
      <!-- 面包屑导航 -->
      <div class="breadcrumb">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/merchant' }">商家中心</el-breadcrumb-item>
          <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      
      <!-- 主要内容 -->
      <main class="main-content">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </main>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { 
  DataLine, 
  Goods, 
  List, 
  Service, 
  Ticket, 
  Setting, 
  ArrowDown 
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/modules/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 导航菜单项
const navItems = [
  { title: '仪表盘', path: '/merchant/dashboard', icon: 'DataLine' },
  { title: '商品管理', path: '/merchant/goods', icon: 'Goods' },
  { title: '订单管理', path: '/merchant/orders', icon: 'List' },
  { title: '售后管理', path: '/merchant/after_sale', icon: 'Service' },
  { title: '优惠券', path: '/merchant/coupons', icon: 'Ticket' },
  { title: '店铺设置', path: '/merchant/settings', icon: 'Setting' }
]

// 当前激活的菜单
const activeMenu = computed(() => {
  const path = route.path
  // 处理子路由情况
  if (path.includes('/merchant/goods/')) return '/merchant/goods'
  if (path.includes('/merchant/orders/')) return '/merchant/orders'
  if (path.includes('/merchant/after_sale/')) return '/merchant/after_sale'
  return path
})

// 当前页面标题
const currentPageTitle = computed(() => {
  const path = route.path
  const pathMap = {
    '/merchant': '仪表盘',
    '/merchant/dashboard': '仪表盘',
    '/merchant/goods': '商品管理',
    '/merchant/goods/add': '添加商品',
    '/merchant/orders': '订单管理',
    '/merchant/after_sale': '售后管理',
    '/merchant/coupons': '优惠券',
    '/merchant/settings': '店铺设置'
  }
  
  if (path.includes('/merchant/orders/')) {
    return '订单详情'
  } else if (path.includes('/merchant/goods/edit/')) {
    return '编辑商品'
  } else if (path.includes('/merchant/after_sale/')) {
    return '售后详情'
  }
  
  return pathMap[path] || '商家中心'
})

// 访问店铺
const goToShop = () => {
  // 跳转到店铺前台页面
  router.push('/shop/1') // 这里的ID需要替换为实际的店铺ID
}

// 退出登录
const logout = async () => {
  await userStore.logout()
  router.push('/login')
}

onMounted(() => {
  // 可以在这里加载商家信息等
})
</script>

<style scoped>
.merchant-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8f4e8; /* 米色背景 */
}

.merchant-header {
  background-color: #ff4d4f; /* 红色顶部导航 */
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-img {
  width: 36px;
  height: 36px;
  border-radius: 50%;
}

.logo h2 {
  margin: 0;
  font-size: 20px;
  color: white;
}

.nav-menu {
  display: flex;
  gap: 20px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 5px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  padding: 0 10px;
  height: 60px;
  transition: all 0.3s;
}

.nav-item:hover, .nav-item.active {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 5px;
  color: white;
  cursor: pointer;
}

.content-container {
  flex: 1;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}

.breadcrumb {
  background-color: white;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.main-content {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  min-height: calc(100vh - 160px);
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 